<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 左侧插画 -->
      <div class="illustration">
        <img src="@/assets/直通车.svg" alt="login illustration">
      </div>

      <!-- 右侧表单 -->
      <div class="form-wrapper">
        <div class="header">
          <h1>校园职通车管理系统</h1>
        </div>

        <el-form ref="loginForm" :model="form" :rules="rules" @keyup.enter.native="handleLogin">
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input v-model="form.password" type="password" placeholder="请输入登录密码" prefix-icon="el-icon-lock"
              show-password></el-input>
          </el-form-item>

          <!-- 角色选择 -->
          <el-form-item prop="role">
            <el-select v-model="form.role" placeholder="请选择角色" class="role-select" popper-class="role-select-dropdown">
              <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>

          <!-- 登录按钮 -->
          <el-form-item>
            <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin">登 录</el-button>
          </el-form-item>
        </el-form>

        <div class="footer">
          <p>© 2025 高校就业管理系统 v1.0</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get } from '@/request/request.js'

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
        role: '管理员'
      },
      roleOptions: [
        { label: '管理员', value: '管理员' },
        { label: '班主任', value: '班主任' },
        { label: '学生', value: '学生' }
      ],
      rules: {
        username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        role: [{ required: true, message: '请选择登录角色', trigger: 'change' }]
      },
      loading: false
    }
  },
  methods: {
    // 处理登录
    async handleLogin() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true


        const params = {
          username: this.form.username,
          password: this.form.password,
          role: this.form.role
        }
        // console.log(params);

        // 调用登录接口
        const res = await get('/user/login', params)
        localStorage.setItem('loginUsername', res.data.username)
        localStorage.setItem('currentrole', res.data.role)
        console.log(res);
        

        // 存储token
        // localStorage.setItem('token', data.token)

        // 跳转到首页
        if (res.code === 200) {
          this.$router.push({ path: '/huanying' })
        } else {
          this.$message.error('登录失败')
        }

      } catch (error) {
        console.error('登录失败:', error)
        this.$message.error(error.response?.data?.message || '登录失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.role-select {
  width: 100%;

  ::v-deep {
    .el-input__inner {
      height: 52px;
      border: 1px solid #DCDFE6;
      border-radius: 8px;
      font-size: 16px;
      padding-left: 44px;
      color: #606266;
      transition: all 0.3s ease;
      background: #FFFFFF;

      &:hover {
        border-color: #C0C4CC;
      }

      &:focus {
        border-color: #409EFF;
        box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
      }
    }

    .el-input__suffix {
      right: 12px;

      .el-icon-arrow-up {
        color: #909399;
        font-size: 14px;
        transition: transform 0.3s;

        &::before {
          content: "\e78f";
          /* 替换为Element UI的下拉图标 */
        }
      }
    }
  }
}

/* 下拉菜单弹出层样式 */
:deep(.role-select-dropdown) {
  border-radius: 8px !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12) !important;
  margin-top: 8px !important;

  .el-select-dropdown__item {
    font-size: 16px;
    height: 48px;
    line-height: 48px;
    padding: 0 20px;
    color: #606266;
    transition: all 0.2s;

    &.selected {
      color: #409EFF;
      background: #f5f7fa;
    }

    &:hover {
      background-color: #f5f7fa;
    }

    &.is-selected {
      background-color: rgba(64, 158, 255, 0.08);
    }
  }

  .popper__arrow {
    display: none !important;
  }
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(150deg, #f8f9fa 0%, #e9ecef 100%);
}

.login-box {
  display: flex;
  width: 1200px;
  height: 600px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.08);
  overflow: hidden;

  // 插画区域
  .illustration {
    flex: 1;
    background: #f8f9fb;
    display: flex;
    align-items: center;
    padding: 80px;

    img {
      width: 100%;
      height: auto;
      object-fit: contain;
    }
  }

  // 表单区域
  .form-wrapper {
    flex: 1;
    max-width: 480px;
    padding: 80px;
    display: flex;
    flex-direction: column;
    justify-content: center;

    .header {
      margin-bottom: 48px;

      h1 {
        font-size: 32px;
        color: #1a1a1a;
        margin-bottom: 12px;
        font-weight: 600;
      }

      p {
        color: #666;
        font-size: 16px;
      }
    }

    .el-form {
      .el-form-item {
        margin-bottom: 32px;

        &:last-child {
          margin-bottom: 0;
        }
      }

      .el-input {
        ::v-deep .el-input__inner {
          height: 52px;
          border-radius: 8px;
          font-size: 16px;
          padding-left: 44px;
        }

        .el-icon-user,
        .el-icon-lock {
          font-size: 20px;
          margin-left: 8px;
        }
      }

      .login-btn {
        width: 100%;
        height: 52px;
        font-size: 18px;
        letter-spacing: 2px;
        border-radius: 8px;
        background: #1a73e8;
        transition: all 0.2s;

        &:hover {
          background: #1557b0;
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(26, 115, 232, 0.3);
        }
      }
    }

    .footer {
      margin-top: 40px;
      color: #999;
      font-size: 14px;
      text-align: center;
    }
  }
}

// 响应式调整
@media (max-width: 1200px) {
  .login-box {
    width: 100%;
    height: auto;
    margin: 20px;

    .illustration {
      padding: 40px;
    }

    .form-wrapper {
      padding: 60px 40px;
    }
  }
}

@media (max-width: 768px) {
  .login-box {
    flex-direction: column;
    height: auto;

    .illustration {
      padding: 40px;

      img {
        max-width: 400px;
      }
    }

    .form-wrapper {
      max-width: 100%;
      padding: 40px 30px;
    }
  }
}
</style>